package com.answer.service.impl;

import com.answer.mapper.AppMapper;
import com.answer.model.dto.app.*;
import com.answer.model.entity.App;
import com.answer.model.reslut.ResultPage;
import com.answer.model.vo.AppVo;
import com.answer.service.AppService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author haoshuai
 * @description 针对表【app(应用)】的数据库操作Service实现
 * @createDate 2024-06-29 15:23:08
 */
@Service
public class AppServiceImpl extends ServiceImpl<AppMapper, App>
        implements AppService {


    @Override
    public Boolean addApp(AppAddDTO appAddDTO) {
        App app = new App();
        BeanUtils.copyProperties(appAddDTO, app);
        int i = baseMapper.insert(app);
        return i == 1;

    }

    @Override
    public ResultPage<AppVo> queryAll(AppQueryDTO appQueryDTO) {
        Page page = new Page(appQueryDTO.getCurrent(), appQueryDTO.getSize());
        baseMapper.selectPage(page, getQueryWrapper(appQueryDTO));
        List<App> apps = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long total = page.getTotal();
        List<AppVo> appVoList = new ArrayList<>();
        for (App app : apps) {
            AppVo appVo = new AppVo();
            BeanUtils.copyProperties(app, appVo);
            appVoList.add(appVo);
        }

        ResultPage resultPage = new ResultPage();
        resultPage.setRecords(appVoList);
        resultPage.setCurrent(current);
        resultPage.setPages(pages);
        resultPage.setTotal(total);
        return resultPage;
    }

    @Override
    public AppVo queryOneById(Long id) {

        App app = baseMapper.selectById(id);
        AppVo appVo = new AppVo();
        BeanUtils.copyProperties(app, appVo);
        return appVo;
    }

    @Override
    public Boolean updateApp(AppUpdateDTO appUpdateDTO) {
        App app = new App();
        BeanUtils.copyProperties(appUpdateDTO, app);
        int i = baseMapper.updateById(app);
        return i == 1;
    }

    @Override
    public Boolean editApp(AppEditDTO appEditDTO) {
        App app = new App();
        BeanUtils.copyProperties(appEditDTO, app);
        int i = baseMapper.updateById(app);
        return i == 1;
    }

    @Override
    public Boolean reviewApp(AppReviewDTO appReviewDTO) {
        App app = new App();
        BeanUtils.copyProperties(appReviewDTO, app);
        // todo 当前用户
        // app.setReviewerId(11L);
        app.setReviewTime(new Date());
        return updateById(app);
    }

    @Override
    public List<HashMap<String, String>> getAppIdAndNameMap() {
        LambdaQueryWrapper<App> wrapper = new LambdaQueryWrapper();
        wrapper.select(App::getId, App::getAppName);
        List<App> apps = baseMapper.selectList(wrapper);

        List<HashMap<String, String>> res = new ArrayList<>();
        if (apps != null && !apps.isEmpty()) {
            for (App app : apps) {
                HashMap<String, String> hashMap = new HashMap<>();
                Long id = app.getId();
                String appName = app.getAppName();
                hashMap.put("value", id.toString());
                hashMap.put("label", appName);
                res.add(hashMap);
            }
        } else {

        }
        return res;
    }


    private LambdaQueryWrapper<App> getQueryWrapper(AppQueryDTO appQueryDTO) {
        Long userId = appQueryDTO.getUserId();
        LambdaQueryWrapper<App> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), App::getUserId, userId);
        return queryWrapper;
    }

}




